//
//  JZBMineViewController.m
//  JZB
//
//  Created by  on 2022/8/29.
//

#import "JZBMineViewController.h"
#import "JZBMineCell.h"
#import "JZBMineInfoViewController.h"
#import "JZBChangePSWViewController.h"
#import "JZBFeedbackViewController.h"
#import "JZBAboutViewController.h"
#import "JZBWebViewController.h"

@interface JZBMineViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *avatar;
@property (strong, nonatomic) IBOutlet UIView *headerVeiw;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *logoutBtn;
@property (strong, nonatomic) IBOutlet UILabel *account;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *nameBottom;
@property (strong, nonatomic) IBOutlet UIButton *nameBtn;

@end

@implementation JZBMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionForUpdate:) name:notifyUserUpdate object:nil];
}
- (void)initUI
{
    [self actionForUpdate:nil];
    self.array = @[@[@"我的资料",@"修改密码"],@[@"反馈建议",@"关于",@"隐私协议"]];
    self.tableviewstyle = UITableViewStyleGrouped;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.scrollEnabled = NO;
    [self.tableView registerNib:[JZBMineCell nib] forCellReuseIdentifier:[JZBMineCell ID]];
    for (NSLayoutConstraint *c in self.view.constraints) {
        if ([c.firstItem isEqual:self.tableView]) {
            c.active = NO;
        }
    }
    NSLayoutConstraint *top = [self.tableView a1:NSLayoutAttributeTop e:NSLayoutRelationEqual t:self.headerVeiw a2:NSLayoutAttributeBottom c:-20];
    NSLayoutConstraint *left = [self.tableView a1:NSLayoutAttributeLeft e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeLeft c:20];
    NSLayoutConstraint *bottom = [self.tableView a1:NSLayoutAttributeBottom e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeBottom c: -20];
    NSLayoutConstraint *right = [self.tableView a1:NSLayoutAttributeRight e:NSLayoutRelationEqual t:self.view a2:NSLayoutAttributeRight c:-20];
    [self.view addConstraints:@[top,left,bottom,right]];
    
    self.cancelBtn = [UIButton new];
    self.cancelBtn.textHexColor = @"#FF5050";
    self.cancelBtn.backGroudHexColor = @"#EEEEEE";
    self.cancelBtn.cornerRadius = 10;
    [self.cancelBtn setTitle:LS(@"注销账号") forState:UIControlStateNormal];
    [self.cancelBtn addTarget:self action:@selector(actionForCancelBtn) forControlEvents:UIControlEventTouchUpInside];
    
    self.logoutBtn = [UIButton new];
    self.logoutBtn.textHexColor = @"#FF5050";
    self.logoutBtn.backGroudHexColor = @"#FFFFFF";
    self.logoutBtn.cornerRadius = 10;
    self.logoutBtn.borderWidth = 1;
    self.logoutBtn.borderHexColor = @"#FF5050";
    [self.logoutBtn setTitle:LS(@"退出登录") forState:UIControlStateNormal];
    [self.logoutBtn addTarget:self action:@selector(actionForLogoutBtn) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 登录后更新
- (void)actionForUpdate:(NSNotification *)notifi{
    [UIImage imageWithURL:APP.user.headImg block:^(UIImage * _Nonnull image) {
        self.avatar.image = image;
    }];
    
    NSString *str = [APP.user.ID isEqualToString:defaulUserID] ? LS(@"去登录") : APP.user.username;
    [self.nameBtn setTitle:str forState:UIControlStateNormal];
    self.account.text = APP.user.ID;
    self.account.hidden = [APP.user.ID isEqualToString:defaulUserID];
    self.nameBottom.constant = [APP.user.ID isEqualToString:defaulUserID] ? 13 : -3;
    self.backBtn.hidden = YES;
    [self.tableView reloadData];
}
#pragma mark - 退出登录
- (void)actionForLogoutBtn{
    [self alert:@"提示" msg:LS(@"确定退出吗？") left:nil right:^{
        APP.user = nil;
        [JZBUser bg_clear:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:notifyUserUpdate object:nil];
    }];
}
#pragma mark - 注销账号
- (void)actionForCancelBtn{
    [self alert:@"提示" msg:LS(@"注销账号后，账号将不存在，是否继续？") left:nil right:^{
        [CCHttp get:api_delUser para:nil header:nil success:^(id  _Nonnull response) {
            APP.user = nil;
            [JZBUser bg_clear:nil];
            [JZBBook bg_clear:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:notifyUserUpdate object:nil];
        } fail:^(NSError * _Nonnull error) { }];
    }];
}
#pragma mark - 登录
- (IBAction)actionForGotoLogin:(id)sender {
    if ([APP.user.ID isEqualToString:defaulUserID]) {
        JZBLoginViewController *vc = [JZBLoginViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (BOOL)gotoLogin{
    if ([APP.user.ID isEqualToString:defaulUserID]) {
        [self  alert:LS(@"提示") msg:LS(@"您当前未登录，是否去登录？") left:nil right:^{
            [self actionForGotoLogin:self.nameBtn];
        }];
        return NO;
    }
    return YES;
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.array.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  [self.array[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JZBMineCell *cell = [tableView dequeueReusableCellWithIdentifier:[JZBMineCell ID]];
    NSArray *array = self.array[indexPath.section];
    cell.name.text = LS(array[indexPath.row]);
    cell.line.hidden = indexPath.row == [array count] - 1;
    return  cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return (section == 1 && ![APP.user.ID isEqualToString:defaulUserID]) ? 140 : 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return section == 1 ? 20 : 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 1 && ![APP.user.ID isEqualToString:defaulUserID]) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 140)];
        view.backgroundColor = [UIColor clearColor];
        self.cancelBtn.frame = CGRectMake(0, 20, SCREEN_WIDTH - 40, 50);
        self.logoutBtn.frame = CGRectMake(0, 90, SCREEN_WIDTH - 40, 50);
        [view addSubview:self.cancelBtn];
        [view addSubview:self.logoutBtn];
        return view;
    }
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
        view.backgroundColor = [UIColor clearColor];
        return view;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * str = self.array[indexPath.section][indexPath.row];
    if ([str isEqualToString:@"我的资料"] && [self gotoLogin]) {
        JZBMineInfoViewController * vc= [JZBMineInfoViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if([str isEqualToString:@"修改密码"] && [self gotoLogin]){
        JZBChangePSWViewController * vc= [JZBChangePSWViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if([str isEqualToString:@"反馈建议"]){
        JZBFeedbackViewController * vc= [JZBFeedbackViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([str isEqualToString:@"关于"]){
        JZBAboutViewController * vc= [JZBAboutViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([str isEqualToString:@"隐私协议"]){
        JZBWebViewController * vc= [JZBWebViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array = self.array[indexPath.section];
    if (indexPath.row == 0 || indexPath.row == array.count - 1) {
        UIBezierPath *path;
        if (indexPath.row == 0) {
            path = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
            
        }else{
            path = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
        }
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.frame = cell.bounds;
        layer.path = path.CGPath;
        cell.layer.mask = layer;
    }else{
        cell.layer.mask = nil;
    }
}
@end

